package learn_concurrent._4;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class T4 {
	static final Logger logger = LoggerFactory.getLogger(T4.class);

	static final Object room = new Object();
	static boolean hasCigarette = false;
	static boolean hasTakeout = false;
	public static void main(String[] args) throws InterruptedException {
		
		new Thread(() -> {
			synchronized (room) {
				logger.info("有烟没？{}", hasCigarette);
				if(!hasCigarette) {
					logger.info("没烟，先歇会！");
					try {
						room.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				logger.info("有烟没？{}", hasCigarette);
				if(hasCigarette) {
					logger.info("可以开始干活了！");
				}
			}
		}, "小南").start();
		
		for(int i = 0; i < 5; i++) {
			new Thread(() -> {
				synchronized (room) {
					logger.info("可以开始干活了！");
				}
			}, "其他人" + i).start();
		}
		
		
		Thread.sleep(1000);
		new Thread(() -> {
			synchronized (room) {
				hasCigarette = true;
				logger.info("烟送到了！");
				room.notify();
			}
		}, "送烟的").start();
		
		
		
	}

}
